package com.zx.viewlearn.views;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.zx.viewlearn.R;
import com.zx.viewlearn.model.Point;
import com.zx.viewlearn.utils.ViewUtils;

/**
 * Created by zhoux on 2018/1/8.
 * 注释：自定义view onMeasure onDraw学习
 */

public class MyView5 extends View {
    Paint mPaint;

    public MyView5(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        setMeasuredDimension(ViewUtils.getMySize(widthMeasureSpec,180),
                ViewUtils.getMySize(heightMeasureSpec,180));
    }

    @Override
    protected void onDraw(Canvas canvas) {

        if (cy1 == -1) {
            drawView(canvas);
            move1();
        } else {
            drawView(canvas);

        }
    }

    private void drawView(Canvas canvas) {
        mPaint.setColor(getResources().getColor(R.color.colorAccent));
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(cx1, cy1, 100, mPaint);


        mPaint.setStrokeWidth((float) 3.0);              //线宽
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(getResources().getColor(R.color.colorPrimary));
        canvas.drawCircle(300, 100, 100, mPaint);


        mPaint.setStrokeWidth((float) 30.0);              //线宽
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(getResources().getColor(R.color.colorPrimaryDark));
        canvas.drawRect(415, 0, 600, 200, mPaint);
    }

    float cx1 = 100;
    float cy1 = -1;

    public float getCx1() {
        return cx1;
    }

    public void setCx1(float cx1) {
        this.cx1 = cx1;
    }

    public float getCy1() {
        return cy1;
    }

    public void setCy1(float cy1) {
        this.cy1 = cy1;
    }

    private void move1() {//属性动画改变y轴位置
        ObjectAnimator mObjectAnimator = ObjectAnimator.ofFloat(this, "cy1", 100, 300);
        mObjectAnimator.setRepeatMode(ValueAnimator.REVERSE);
        mObjectAnimator.setRepeatCount(100);
        mObjectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                invalidate();//重新绘制，会调用onDraw()
            }
        });
        mObjectAnimator.start();

    }


}
